package cn.doctorpda.conference.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.checkmeeting.CheckMeetingInfoItem;
import cn.doctorpda.conference.utils.ScreenUtils;

/**
 * Created by huangjinlong on 2016/3/29.
 */
public class CheckMeetingAdapter extends RecyclerView.Adapter<CheckMeetingAdapter.ViewHolder> {

    //    private List<String> datas = new ArrayList<>();
    private ArrayList<CheckMeetingInfoItem> meetingList = new ArrayList<CheckMeetingInfoItem>();
    private OnItemClickListener listener;
    private Context context;


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, CheckMeetingInfoItem data);
    }

    public void clear() {
        this.meetingList.clear();
        notifyDataSetChanged();
    }

    public void addAll(ArrayList<CheckMeetingInfoItem> meetingList) {
        this.meetingList.addAll(meetingList);
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_check_meeting_new, parent, false);
        LinearLayout.LayoutParams la = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(parent.getContext()) / 2, ScreenUtils.getScreenHeight(parent.getContext()) / 4);
        view.setLayoutParams(la);
        this.context = parent.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final int pos = position;
        final CheckMeetingInfoItem itemInfo = meetingList.get(pos);
//        holder.tv.setText(data);
//        ImageUtils.loadImage(context, itemInfo.getCLIENT_BANNER(), holder.imvMeetingPic);

        if (TextUtils.isEmpty(itemInfo.getPROVICE_NAME())) {
            itemInfo.setPROVICE_NAME("");
        }
        if (TextUtils.isEmpty(itemInfo.getCITY_NAME())) {
            itemInfo.setCITY_NAME("");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String date_start = "", date_stop = "";
        if (!TextUtils.isEmpty(itemInfo.getSTART_TIME()) && itemInfo.getSTART_TIME().length() > 10) {
            try {
                date_start = sdf.format(Date.valueOf(itemInfo.getSTART_TIME().substring(0, 9)));
//                holder.tvMeetingDate.setText(date_start);
                holder.tvMeetingDate.setText(date_start);
            } catch (Exception e) {
                e.printStackTrace();
                holder.tvMeetingDate.setText(itemInfo.getSTART_TIME());
            }
        } else {
            holder.tvMeetingDate.setText("待确定");
        }
        if (!TextUtils.isEmpty(itemInfo.getEND_TIME()) || itemInfo.getEND_TIME().length() > 10) {
            try {
                date_stop = sdf.format(Date.valueOf(itemInfo.getEND_TIME().substring(0, 9)));
//                holder.tvMeetingDate.setText(date_start + "-" + date_stop.substring(8, 11));
            } catch (Exception e) {
                holder.tvMeetingDate.setText(itemInfo.getSTART_TIME());
            }
        } else {
            holder.tvMeetingDate.setText(itemInfo.getSTART_TIME());
        }
        holder.tvMeetingAddress.setText(itemInfo.getPROVICE_NAME() + itemInfo.getCITY_NAME());
        holder.tvMeetingState.setText(itemInfo.getCONSTATUS());
        if (itemInfo.getCONSTATUS().equals("报名中")) {
            holder.tvMeetingState.setBackgroundResource(R.drawable.btn_item_meeting_details);
        } else if (itemInfo.getCONSTATUS().equals("报名截止")) {
            holder.tvMeetingState.setBackgroundResource(R.drawable.btn_item_meeting_state);
        }
        holder.tvMeetingTitle.setText(itemInfo.getNAME());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, meetingList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return meetingList.size();
    }

    //item元素
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvMeetingTitle, tvMeetingState, tvMeetingDate, tvMeetingAddress;//会议标题，会议状态，会议时间，会议地址
//        ImageView imvMeetingPic;//会议图片

        public ViewHolder(View itemView) {
            super(itemView);
            tvMeetingTitle = (TextView) itemView.findViewById(R.id.txt_item_title);
            tvMeetingState = (TextView) itemView.findViewById(R.id.txt_item_meeting_state);
            tvMeetingDate = (TextView) itemView.findViewById(R.id.txt_item_date);
            tvMeetingAddress = (TextView) itemView.findViewById(R.id.txt_item_address);

//            imvMeetingPic = (ImageView) itemView.findViewById(R.id.imv_meeting);
        }
    }
}
